package framework.event
{
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;

	public class EventToken
	{
		public static var defaultHowManyTimes:int = 1;

		public static var eventTokens:Dictionary = new Dictionary(true);

		public var dispatcher:IEventDispatcher;

		public var eventType:String;

		public var listener:Function;

		public var suspended:Boolean;

		public var times:int;

		public var unlimited:Boolean;

		public var useCapture:Boolean;

		public function stop():void
		{
			dispatcher.removeEventListener(eventType, handler, useCapture);
			delete eventTokens[dispatcher];
		}

		public function handler(e:Event):void
		{
			if (suspended)
				return;

			listener(e);

			if (unlimited)
				return;

			times--;
			if (!times)
			{
				stop();
			}

		}
	}
}